/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once
	
/*-----------------------------------------------------------------------------
	TRANSFORM.H
-----------------------------------------------------------------------------*/

class	ETransform {
 	public:
						ETransform			( void ) : position(EPoint::kOrigin), orient(EQuaternion::kIdentity) {}
						ETransform			( const EPoint &pos, const EQuaternion &orient=EQuaternion::kIdentity ) : position(pos), orient(orient) {}
						~ETransform			( void ) {}	  

		EPoint			GetPosition			( void ) const { return position; }
		EQuaternion		GetOrient			( void ) const { return orient; }
		void			SetPosition			( const EPoint &pos	)			{ this->position = pos;		}
		void			SetOrient			( const EQuaternion	&orient )	{ this->orient   = orient;	}

		void			SetPose				( const EPoint &pos, const EQuaternion &orient=EQuaternion::kIdentity );
		void			GetPose				( EPoint &pos, EQuaternion &orient ) const;
		
		EMatrix			ComputeMatrix		( void ) const;
										
		EVector			GetVector			( const EVector &local_vector ) const;
		EPoint			GetPoint			( const EPoint &local_point ) const;
		
		EVector			GetLocalVector		( const EVector &global_vector ) const;
		EPoint			GetLocalPoint		( const EPoint &global_point ) const;
		
		ETransform		ReverseTransform	( void ) const;
		
	public:
	
		static	ETransform	FromAnglesZB	( const EPoint &origin, float yaw, float pitch, float roll );
		static	ETransform	FromAnglesXF	( const EPoint &origin, float yaw, float pitch, float roll );
		static	ETransform	LookAtZB		( const EPoint &origin, const EPoint &target, const EVector &up );
		static	ETransform	LookAtXF		( const EPoint &origin, const EPoint &target, const EVector &up );
		static	ETransform	AddTransforms	( const ETransform &parent, const ETransform &child );
		
	protected:
		EPoint		position;
		EQuaternion	orient;
	};


//
// ETransform::SetPose()
//
inline void ETransform::SetPose( const EPoint &pos, const EQuaternion &orient/*=EQuaternion::kIdentity*/ )
{
	this->position = pos;		
	this->orient   = orient;	
}


//
// ETransform::GetPose()
//
inline void ETransform::GetPose( EPoint &pos, EQuaternion &orient ) const
{
	pos		= this->position;		
	orient  = this->orient;
}


//
// ETransform::ComputeMatrix()
//
inline EMatrix ETransform::ComputeMatrix( void ) const
{
	EMatrix	T	=	EMatrix::Translate( position.x, position.y, position.z );

	float	wx, wy, wz;
	float	xx, yy, yz;
	float	xy, xz, zz;
	float	x2, y2, z2;

	x2 = orient.x + orient.x;
	y2 = orient.y + orient.y;
	z2 = orient.z + orient.z;

	xx = orient.x * x2;
	xy = orient.x * y2;
	xz = orient.x * z2;

	yy = orient.y * y2;
	yz = orient.y * z2;
	zz = orient.z * z2;

	wx = orient.w * x2;
	wy = orient.w * y2;
	wz = orient.w * z2;

	EMatrix R;

    R(0,0) = 1.0f - (yy + zz);
    R(1,0) = xy - wz;
    R(2,0) = xz + wy;

    R(0,1) = xy + wz;
    R(1,1) = 1.0f - (xx + zz);
    R(2,1) = yz - wx;

    R(0,2) = xz - wy;
    R(1,2) = yz + wx;
    R(2,2) = 1.0f - (xx + yy);
    
	return R * T;
}


//
// ETransform::GetVector()
//
inline EVector ETransform::GetVector( const EVector &local_vector ) const
{
	return local_vector.Rotate(orient);
}


//
// ETransform::GetPoint()
//
inline EPoint ETransform::GetPoint( const EPoint &local_point ) const
{
	return local_point.Rotate(orient) + EVector(position);
}

//
// ETransform::GetLocalVector()
//
inline EVector ETransform::GetLocalVector( const EVector &global_vector ) const
{
	return global_vector.Rotate(orient.Conjugate());
}


//
// ETransform::GetLocalPoint()
//
inline EPoint ETransform::GetLocalPoint( const EPoint &global_point ) const
{
	return (global_point - EVector(position)).Rotate(orient.Conjugate());
}


//
// ETransform::ReverseTransform()
//
inline ETransform ETransform::ReverseTransform( void ) const
{
	EQuaternion inv_q = orient.Inverse();
	EPoint		inv_p = EPoint(-position.x, -position.y, -position.z);

	inv_p = inv_p.Rotate(inv_q);
	
	return ETransform(inv_p, inv_q);
}
